{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "量化投资\n"
     ]
    }
   ],
   "source": [
    "xa = 'hello world'\n",
    "xa= '量化投资'\n",
    "print(xa)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 字符串运算符\n",
    "![image.png](attachment:image.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world\n",
      "hello worldhello world\n",
      "h\n",
      "hel\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "xa='hello '+ 'world'\n",
    "print(xa)\n",
    "print(xa*2)\n",
    "print(xa[0])\n",
    "print(xa[0:3])\n",
    "print(\"hello\" in xa)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 字符串格式化\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "今天天气不错，出去玩\n"
     ]
    }
   ],
   "source": [
    "print('今天天气%s，%s' % ('不错','出去玩'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 三引号\n",
    "三引号允许一个字符串跨多行，字符串中可以包含换行符、制表符以及其他特殊字符。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "这是一个多行字符串的实例\n",
      "多行字符串可以使用制表符\n",
      "TAB ( \\t )。\n",
      "也可以使用换行符 [ \\n ]。\n",
      "\n"
     ]
    }
   ],
   "source": [
    "para_str = r\"\"\"这是一个多行字符串的实例\n",
    "多行字符串可以使用制表符\n",
    "TAB ( \\t )。\n",
    "也可以使用换行符 [ \\n ]。\n",
    "\"\"\"\n",
    "print (para_str)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 字符串内建函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world\n"
     ]
    }
   ],
   "source": [
    "xa='hello world'\n",
    "print(xa)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# capitalize()\n",
    "将字符串的第一个字符转换为大写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello world\n"
     ]
    }
   ],
   "source": [
    "print(xa.capitalize())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# center(width, fillchar)\n",
    "返回一个指定的宽度 width 居中的字符串，fillchar 为填充的字符，默认为空格。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "*****hello world*****\n"
     ]
    }
   ],
   "source": [
    "print(xa.center(21,'*'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# count(str, beg= 0,end=len(string))\n",
    "返回 str 在 string 里面出现的次数，如果 beg 或者 end 指定则返回指定范围内 str 出现的次数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xa.count('l')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# encode(encoding='UTF-8',errors='strict')\n",
    "以 encoding 指定的编码格式编码字符串，如果出错默认报一个ValueError 的异常，除非 errors 指定的是'ignore'或者'replace'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'bytes'>\n"
     ]
    }
   ],
   "source": [
    "print(type(xa.encode('utf-8')))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# bytes.decode(encoding=\"utf-8\", errors=\"strict\")\n",
    "使用 bytes 对象的 decode() 方法来解码给定的 bytes 对象，这个 bytes 对象可以由 str.encode() 来编码返回。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'str'>\n"
     ]
    }
   ],
   "source": [
    "print(type(xa.encode('utf-8').decode('utf-8')))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# endswith(suffix, beg=0, end=len(string))\n",
    "检查字符串是否以 obj 结束，如果beg 或者 end 指定则检查指定的范围内是否以 obj 结束，如果是，返回 True,否则返回 False."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "print(xa.endswith('d'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# \texpandtabs(tabsize=8)\n",
    "把字符串 string 中的 tab 符号转为空格，tab 符号默认的空格数是 8 。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "he is xiaoming\n"
     ]
    }
   ],
   "source": [
    "print('he is\\txiaoming'.expandtabs(1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# find(str, beg=0 end=len(string))\n",
    "检测 str 是否包含在字符串中，如果指定范围 beg 和 end ，则检查是否包含在指定范围内，如果包含返回开始的索引值，否则返回-1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world 4\n"
     ]
    }
   ],
   "source": [
    "print(xa,xa.find('o'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# isalnum()\n",
    "如果字符串至少有一个字符并且所有字符都是字母或数字则返 回 True,否则返回 False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world False\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "print(xa,xa.isalnum())\n",
    "print('123'.isalnum())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# isalpha()\n",
    "如果字符串至少有一个字符并且所有字符都是字母则返回 True, 否则返回 False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world False\n",
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "print(xa,xa.isalpha())\n",
    "print('abc'.isalpha())\n",
    "print('123'.isalpha())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# isdigit()\n",
    "如果字符串只包含数字则返回 True 否则返回 False.."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world False\n",
      "False\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "print(xa,xa.isdigit())\n",
    "print('abc'.isdigit())\n",
    "print('123'.isdigit())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# islower()\n",
    "如果字符串中包含至少一个区分大小写的字符，并且所有这些(区分大小写的)字符都是小写，则返回 True，否则返回 False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world True\n",
      "Hello world False\n"
     ]
    }
   ],
   "source": [
    "print(xa,xa.islower())\n",
    "print(xa.capitalize(),xa.capitalize().islower())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# isspace()\n",
    "如果字符串中只包含空白，则返回 True，否则返回 False."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world False\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "print(xa,xa.isspace())\n",
    "print('   '.isspace())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# istitle()\n",
    "如果字符串是标题化的(见 title())则返回 True，否则返回 False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world False\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "print(xa,xa.istitle())\n",
    "print('Hello World'.istitle())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# \tisupper()\n",
    "如果字符串中包含至少一个区分大小写的字符，并且所有这些(区分大小写的)字符都是大写，则返回 True，否则返回 False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world False\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "print(xa,xa.istitle())\n",
    "print('HELLO WORLD'.isupper())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# join(seq)\n",
    "以指定字符串作为分隔符，将 seq 中所有的元素(的字符串表示)合并为一个新的字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'str'>\n"
     ]
    }
   ],
   "source": [
    "print(type('*'.join(['li','zhang','chen'])))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# len(string)\n",
    "返回字符串长度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "11 hello world\n"
     ]
    }
   ],
   "source": [
    "print(len(xa),xa)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# \tljust(width[, fillchar])\n",
    "返回一个原字符串左对齐,并使用 fillchar 填充至长度 width 的新字符串，fillchar 默认为空格。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world!!!!!!!!!\n"
     ]
    }
   ],
   "source": [
    "print(xa.ljust(20,'!'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# \tlower()\n",
    "转换字符串中所有大写字符为小写."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world\n"
     ]
    }
   ],
   "source": [
    "print('HELLO WORLD'.lower())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# lstrip()\n",
    "截掉字符串左边的空格或指定字符。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 136,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "8\n",
      "ello\n"
     ]
    }
   ],
   "source": [
    "print(len('     hello   '.lstrip()))\n",
    "print('hello'.lstrip(\"h\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# \tmaketrans()\n",
    "创建字符映射的转换表，对于接受两个参数的最简单的调用方式，第一个参数是字符串，表示需要转换的字符，第二个参数也是字符串表示转换的目标。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "th3s 3s str3ng 2x1mpl2....w4w!!!\n"
     ]
    }
   ],
   "source": [
    "intab = \"aeiou\"\n",
    "outtab = \"12345\"\n",
    "trantab = str.maketrans(intab, outtab)\n",
    "str = \"this is string example....wow!!!\"\n",
    "print (str.translate(trantab))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# \tmax(str)\n",
    "返回字符串 str 中最大的字母。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "w\n"
     ]
    }
   ],
   "source": [
    "print(max(xa))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# min(str)\n",
    "返回字符串 str 中最小的字母。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a\n"
     ]
    }
   ],
   "source": [
    "print(min(\"xa\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# replace(old, new [, max])\n",
    "把 将字符串中的 str1 替换成 str2,如果 max 指定，则替换不超过 max 次。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world heyyo woryd\n"
     ]
    }
   ],
   "source": [
    "print(xa,xa.replace(\"l\",'y'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# \trfind(str, beg=0,end=len(string))\n",
    "类似于 find()函数，不过是从右边开始查找."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world 9\n"
     ]
    }
   ],
   "source": [
    "print(xa,xa.rfind(\"l\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# rjust(width,[, fillchar])\n",
    "返回一个原字符串右对齐,并使用fillchar(默认空格）填充至长度 width 的新字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "wwwwwwwwwhello world\n"
     ]
    }
   ],
   "source": [
    "print(xa.rjust(20,'w'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# \trstrip()\n",
    "删除字符串字符串末尾的空格."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n"
     ]
    }
   ],
   "source": [
    "print(len('hello    '.rstrip()))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# split(str=\"\", num=string.count(str))\n",
    "num=string.count(str)) 以 str 为分隔符截取字符串，如果 num 有指定值，则仅截取 num 个子字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello*world*!\n",
      "['hello', 'world', '!']\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "xb='*'.join(['hello','world',\"!\"])\n",
    "print(xb)\n",
    "print(xb.split('*'))\n",
    "print(xb.count('*'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# \tstartswith(str, beg=0,end=len(string))\n",
    "检查字符串是否是以 obj 开头，是则返回 True，否则返回 False。如果beg 和 end 指定值，则在指定范围内检查。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world False\n"
     ]
    }
   ],
   "source": [
    "print(xa,xa.startswith('e'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# strip([chars])\n",
    "在字符串上执行 lstrip()和 rstrip()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n"
     ]
    }
   ],
   "source": [
    "print(len('   hello   '.strip()))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# swapcase()\n",
    "将字符串中大写转换为小写，小写转换为大写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hELLO wROLD\n"
     ]
    }
   ],
   "source": [
    "print('Hello Wrold'.swapcase())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# title()\n",
    "返回\"标题化\"的字符串,就是说所有单词都是以大写开始，其余字母均为小写(见 istitle())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world Hello World\n"
     ]
    }
   ],
   "source": [
    "print(xa,xa.title())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# \tupper()\n",
    "转换字符串中的小写字母为大写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "HELLO WORLD\n"
     ]
    }
   ],
   "source": [
    "print(xa.upper())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# zfill (width)\n",
    "返回长度为 width 的字符串，原字符串右对齐，前面填充0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "000000000hello world\n"
     ]
    }
   ],
   "source": [
    "print(xa.zfill(20))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# \tisdecimal()\n",
    "检查字符串是否只包含十进制字符，如果是返回 true，否则返回 false。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 135,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "print(xa.isdecimal())\n",
    "print('1233'.isdecimal())"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
